package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 1 班级列表数据的条件分页查询
     */
    @GetMapping
      public  Result Page (ClazzQueryParm clazzQueryParm){

        log.info("查询所有班级数据：{}" ,clazzQueryParm);
        PageResult<Clazz> clazz = clazzService.page(clazzQueryParm);
        return Result.success(clazz);
    }

    /**
     * 2 删除班级
     */
     @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        log.info("删除班级，id={}", id);
        clazzService.deleteById(id);
        return Result.success();
    }
    /**
     * 3 新增班级
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("新增班级数据:{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**·
     * 4 根据id查询班级·(查询回写数据)
     */

    @GetMapping ("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        log.info("查询班级，id={}", id);
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }
    /**
     * 5 修改班级数据
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级数据:{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 6 查询所有班级
     */
    @GetMapping("/list")
    public Result findAll() {
        log.info("查询所有班级");
        List<Clazz> clazz = clazzService.findAll();
        return Result.success(clazz);
    }


}
